﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Data;
using MyWeb.Datamining;
using MyWeb.Datamining.Kmeans;

namespace MyWeb
{
    public partial class DemoKmean : Page
    {
        private readonly UserService _userService=new UserService();
        private readonly StatisticsService _statisticsService = new StatisticsService();
        private readonly DistributionService _distributionService=new DistributionService();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dUser = _userService.User_GetByAll();
                DataTable dUserId = new DataTable();
                dUserId.Columns.Add("User_Id", typeof (string));
                for (int i = 0; i < dUser.Rows.Count; i++)
                {
                    DataRow rowUser = dUserId.NewRow();
                    rowUser["User_Id"] = dUser.Rows[i]["Id"].ToString();
                    dUserId.Rows.Add(rowUser);
                    
                }
                DataTable dDistribution = _distributionService.Distribution_GetByAll();
                DataTable dDistributionId = new DataTable();

                dDistributionId.Columns.Add("Distribution_Id", typeof (string));
               
                for (int i = 0; i < dDistribution.Rows.Count; i++)
                {
                    DataRow rowDis = dDistributionId.NewRow();
                    rowDis["Distribution_Id"] = dDistribution.Rows[i]["Distribution"].ToString();
                    dDistributionId.Rows.Add(rowDis);
                }
                DataTable dStatistics = _statisticsService.Statistics_GetByAll();
                DataTable dViewnumber = new DataTable();
                dViewnumber.Columns.Add("ViewNumber", typeof (string));
               
                for (int i = 0; i < dStatistics.Rows.Count; i++)
                {
                    DataRow rowView = dViewnumber.NewRow();
                    rowView["ViewNumber"] = dStatistics.Rows[i]["ViewNumber"].ToString();
                    dViewnumber.Rows.Add(rowView);
                }
                DataTable dt = _distributionService.Distribution_GetByAll();
                ConvertDataTable kmean = new ConvertDataTable();
                Session["DataUser"] = kmean.DataTableStatistics(dUserId, dDistribution, dViewnumber, dt);
                gvKmean.DataSource = kmean.DataTableStatistics(dUserId, dDistribution, dViewnumber,dt);
                gvKmean.DataBind();
            }
        }
        SqlDataProvider sqlDataProvider=new SqlDataProvider();
        protected void btnKmean_Click(object sender, EventArgs e)
        {
              DataTable dt1 = _distributionService.Distribution_GetByAll();
            DataTable dt = Session["DataUser"] as DataTable;
            int[] iUser=new int[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                iUser[i] = Int32.Parse(dt.Rows[i]["user_id"].ToString());
            }
            KmeanProcess kmeanProcess=new KmeanProcess();
            int[] groupUser = kmeanProcess.Kmean(2, dt);
            for (int i = 0; i < iUser.Length; i++)
            {
                sqlDataProvider.ExecuteNonQuery("insert into Kmeans([user_id],[groupuser]) values('"+iUser[i]+"','"+groupUser[i]+"')");
            }
        }
    }
}